//
//  ZZComposePictureView.swift
//  web
//
//  Created by 张卓 on 2017/2/9.
//  Copyright © 2017年 ZZ. All rights reserved.
//

import UIKit


private let picCellID = "picCellID"
class ZZComposePictureView: UICollectionView {

    //提供保存图片的数组
    var images:[UIImage] = [UIImage]()
    
    //1. 声明闭包
    var closure:(()->())?
    
    
    //共外界调用的方法
    func addImg (image: UIImage){
        //显示配图
        isHidden = false
        //1. 添加到数组
        images.insert(image, at: 0)
        //如果Images.cout 大于 9 以后移除最后面的
        if images.count > 9 {
            images.removeLast()
        }
        //2. 刷新
        reloadData()
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        //定义一个layout
        let flowLayou = UICollectionViewFlowLayout()
        let itemW = (ZZSCREENW - 20 - 10) / 3
        flowLayou.itemSize = CGSize(width: itemW, height: itemW)
        flowLayou.minimumLineSpacing = 5
        flowLayou.minimumInteritemSpacing = 5
        super.init(frame: frame, collectionViewLayout: flowLayou)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //设置视图
    private func setupUI(){
        //默认配图是隐藏的
        isHidden = true
        backgroundColor = UIColor.white
        //设置数据源
        dataSource = self
        //设置点击代理
        delegate = self
        
        register(ZZComposePictureViewCell.self, forCellWithReuseIdentifier: picCellID)
    }
}

extension ZZComposePictureView: UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //如果只有一张图片, cell要显示两个 img.count + 1 
        //如果有0张或者9张. cell 要显示当前, count
//        return images.count
        if images.count == 0 || images.count == 9 {
            return images.count
        }else{
            return images.count+1
        }
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: picCellID, for: indexPath) as! ZZComposePictureViewCell
//        cell.image = images[indexPath.item]
        if indexPath.item == images.count {
            cell.image = nil
        }else {
            cell.image = images[indexPath.item]
        }
        //2. 实例化闭包
        cell.closure = {[weak self]()->() in
            //删除images里面对应的元素
            //4. 回调
            self?.images.remove(at: indexPath.item)
            //判断如果images.count == 0 隐藏
            if self?.images.count == 0 {
                self?.isHidden = true
            }
            //刷新数据
            self?.reloadData()
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        if indexPath.item == images.count {
            //3. 执行闭包
            closure?()
        }
    }
    
    
    
}

class ZZComposePictureViewCell: UICollectionViewCell {
    
    //提供一个属性共外界赋值
    var image:UIImage?{
        didSet{
            
            if image == nil{
                pic.image = UIImage(named: "compose_pic_add")
                pic.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                //隐藏删除按钮
                deleteBut.isHidden = true
            }else{
                
                pic.image = image
                //解决复用问题
                pic.highlightedImage = nil
                //显示
                deleteBut.isHidden = false
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        contentView.addSubview(pic)
//        pic.backgroundColor = ZZRandomColor()
        contentView.addSubview(deleteBut)
        
        //设置约束
        pic.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        deleteBut.snp_makeConstraints { (make) in
            make.top.right.equalTo(contentView)
        }
    }
    
    //1. 定义闭包. 告知父类 删除 images 里面对应的元素
    var  closure:(()->())?
    
    @objc private func buttoClick(){
        //3. 执行闭包
        closure?()
    }
    
    // 懒加载图片
    private lazy var pic : UIImageView = UIImageView()
    
    //删除按钮
    private lazy var deleteBut : UIButton = {
        let bnt = UIButton()
        bnt.setImage(UIImage(named:"compose_photo_close"), for: UIControlState.normal)
        //添加点击时间
        bnt.addTarget(self, action: #selector(buttoClick), for: UIControlEvents.touchUpInside)
        return bnt
    }()
    
    
    
}
